﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

namespace LCA.Admin.FormControls
{
    public partial class DateSelector : System.Web.UI.UserControl
    {
        private static CultureInfo en_US = CultureInfo.GetCultureInfo("en-US");

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public bool ShowHours
        {
            //get { return ViewState["ShowHours"] == null ? false : (bool)ViewState["ShowHours"]; }
            //set { ViewState["ShowHours"] = value; }
            get;
            set;
        }

        public bool Enabled
        {
            get { return txtDatePicker.Enabled; }
            set{ txtDatePicker.Enabled = value; }
        }

        public Unit Width
        {
            get { return txtDatePicker.Width; }
            set { txtDatePicker.Width = value; }
        }

        public DateTime? SelectedValue
        {
            get 
            { 
                DateTime result;
                if (!String.IsNullOrEmpty(txtDatePicker.Text) &&
                    DateTime.TryParseExact(txtDatePicker.Text.ToUpper(), DateFormat, en_US, DateTimeStyles.AllowWhiteSpaces, out result))
                { 
                    return result;
                }
                
                return null;
            }
            set { txtDatePicker.Text = String.Format("{0:" + DateFormat  + "}", value); }
        }

        private string DateFormat
        {
            get { return ShowHours ? "MM/dd/yyyy hh:mm tt" : "MM/dd/yyyy"; }
        }
    }
}